﻿using Known.EntityFramework;
using Microsoft.EntityFrameworkCore;
using Known.SqlSugar;
using SqlSugar;
using SampleWeb;

var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddRazorComponents()
                .AddInteractiveServerComponents();
builder.Services.AddApplication(info =>
{
    info.App.WebRoot = builder.Environment.WebRootPath;
    info.App.ContentRoot = builder.Environment.ContentRootPath;
});
// SqlSugar配置
builder.Services.AddKnownSqlSugar(config =>
{
    config.DbType = DbType.SqlServer;
    config.ConnectionString = builder.Configuration.GetSection("ConnStringSS").Get<string>();
    config.IsAutoCloseConnection = true;
    config.MoreSettings ??= new ConnMoreSettings();
    config.MoreSettings.IsAutoToUpper = false;
    config.AopEvents ??= new AopEvents();
    //config.AopEvents.OnLogExecuting = (sql, pars) =>
    //{
    //    var param = string.Join(",", pars.Select(p => $"{p.ParameterName}={p.Value}"));
    //    Console.WriteLine($"SQL: {sql}, 参数: {param}");
    //};
});
// EFCore配置
//builder.Services.AddKnownEntityFramework(option =>
//{
//    // 配置数据库
//    option.OnConfig = c => c.UseSqlServer(builder.Configuration.GetSection("ConnStringEF").Get<string>());
//    // 在此配置业务库数据模型
//    //option.OnModel = m =>
//    //{
//    //    m.Entity(typeof(YourEntity)).HasKey(x => x.Id);
//    //};
//});
// FreeSql配置
//builder.Services.AddKnownFreeSql(option =>
//{
//    option.UseConnectionString(DataType.SqlServer, builder.Configuration.GetSection("ConnString").Get<string>());
//    //.UseMonitorCommand(cmd => Console.WriteLine($"Sql：{cmd.CommandText}"))
//    //.UseAutoSyncStructure(true); //自动同步实体结构到数据库，只有CRUD时才会生成表
//});

var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error", createScopeForErrors: true);
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseRouting();
app.UseAntiforgery();
app.UseApplication();
app.MapRazorPages();
app.MapRazorComponents<App>()   
   .AddInteractiveServerRenderMode()
   .AddAdditionalAssemblies([.. Config.Assemblies]);
app.Run();